package cn.lg.soar.database.query;

import cn.lg.soar.common.util.lambda.IGetter;
import cn.lg.soar.common.util.lambda.LambdaUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询参数对象
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
@ApiModel("查询参数对象，重复字段字段采用单字母简写")
public class QueryParams {

    public final static String VALUE_JOIN = "~";
    public final static String FIELD_JOIN = "-";

    @ApiModelProperty(value = "过滤条件，格式：字段-条件~值 或 字段1|字段2-条件~值1~值2，多个字段用“|”分隔,多个值用“~”分隔" +
            "例：id-eq~11 或 email|phone-like~150 或 type-in~1~3~7，注意：不支持多个值的条件会把多个值当作一个值", example = "id-in~1~3~9")
    private List<String> f;

    @ApiModelProperty(value = "排序字段，逆序在字段前加“-”", example = "id,-createTime")
    private List<String> orderBy;

    @ApiModelProperty(value = "查询字段，默认查询所有字段", example = "id,createTime")
    private List<String> fields;

    /**
     * 添加条件
     * @param field 字段
     * @param condition 条件
     * @param args 参数
     * @return
     */
    public QueryParams addFilter(String field, ConditionEnum condition, Object...args) {
        if (f == null) {
            f = new ArrayList<>();
        }
        String values = Arrays.stream(args).map(Object::toString).collect(Collectors.joining(VALUE_JOIN));
        f.add(field + FIELD_JOIN + condition.name() + VALUE_JOIN + values);
        return this;
    }

    /**
     * 添加条件
     * @param field 字段
     * @param condition 条件
     * @param args 参数
     * @return
     */
    public <T>QueryParams addFilter(IGetter<T, ?> field, ConditionEnum condition, Object...args) {
        return addFilter(LambdaUtils.getterToFieldName(field), condition, args);
    }

    /**
     * 添加正序排序字段
     * @param field 字段
     * @return
     */
    public QueryParams addOrderByAsc(String field) {
        if (orderBy == null) {
            orderBy = new ArrayList<>();
        }
        orderBy.add(field);
        return this;
    }


    /**
     * 添加正序排序字段
     * @param field 字段
     * @return
     */
    public <T>QueryParams addOrderByAsc(IGetter<T, ?> field) {
        return addOrderByAsc(LambdaUtils.getterToFieldName(field));
    }

    /**
     * 添加逆序排序字段
     * @param field 字段
     * @return
     */
    public QueryParams addOrderByDesc(String field) {
        if (orderBy == null) {
            orderBy = new ArrayList<>();
        }
        orderBy.add("-" + field);
        return this;
    }

    /**
     * 添加逆序排序字段
     * @param field 字段
     * @return
     */
    public <T>QueryParams addOrderByDesc(IGetter<T, ?> field) {
        return addOrderByDesc(LambdaUtils.getterToFieldName(field));
    }

    /**
     * 添加查询字段
     * @param field 字段
     * @return
     */
    public QueryParams addSelect(String field) {
        if (fields == null) {
            fields = new ArrayList<>();
        }
        fields.add(field);
        return this;
    }

    /**
     * 添加查询字段
     * @param field 字段
     * @return
     */
    public <T>QueryParams addSelect(IGetter<T, ?> field) {
        return addSelect(LambdaUtils.getterToFieldName(field));
    }
}
